<template>
  <div class="public">
    <div class="top">
      <van-icon name="arrow-left" @click="back" />
      <p>悦食菜谱</p>
      <van-icon name="wap-home-o" @click="backHome" />
    </div>
  

    <!-- 列表内容 -->
    <div class="Food_list" v-if="this.$store.state.publicList.length != 0">
      <ul>
        <li  @click="fooddetailFn(item.id)" v-for="item in this.$store.state.publicList" :key="item.id">
          <div>
            <img :src="item.img" alt="" />
          </div>
          <div class="message_item">
            <p>{{ item.name }}</p>
            <p class="p_text2">{{ item.tips }}</p>
            <p><van-icon name="star-o" />{{ item.ph }}</p>
            <p><van-icon name="fire-o" />{{ item.pw }}</p>
          </div>
        </li>
      </ul>
    </div>
    <div v-else style="text-align: center; color: #ccc;margin-top:100px">
      <van-loading type="spinner" color="#1989fa" />
      </div>
  </div>
</template>


<script>
import { Food_search } from "../api/index";
export default {
  data() {
    return {
     
    };
    
  },
 
  //  涉及异步用侦听器侦听keyword的变化再运用方法
  created() {
 this.handleRouterChange()
    // console.log(this.$route.query);
    // this.result=this.$route.query
    // this.Food_suggestFn(this.$route.query);
  },
  watch:{
  '$route.query': {  
      handler() {  
        // 当路由查询参数变化时，更新 result 并调用 Food_suggestFn  
        this.handleRouterChange();  
      },  
      deep: true // 如果查询参数是对象或数组，需要深度监听  
    }  
  },
  
  methods: {
     fooddetailFn(id){
     this.$router.push({path:'./foodetail',query:{id}})
     console.log(id);
    },
    handleRouterChange(){
      const queryParam = this.$route.query.keyword
      console.log('关键字',queryParam);
      this.Food_suggestFn(queryParam)
    },
    back() {
      this.$router.go(-1);
      this.$store.state.publicList = "";
    },
    backHome() {
      this.$router.push({ path: "./mycenter" });
    },
    async Food_suggestFn(value) {
      await Food_search(value).then((content) => {
        let { state, result } = content;
        if (state == "success") {
          // console.log(result);
          let { list } = result;
          // console.log(list);
          let data = list.map((item) => ({
            r: item.r,
          }));
          console.log("data", data);
          // 存在r,才渲染
          let filteredList = data.filter(
            (item) => item.hasOwnProperty("r") && item.r !== undefined
          );
          // console.log(filteredList); //过滤掉r为undefined
          let arr = [];
          filteredList.forEach((item) => {
            // console.log('item',item);
            let obj = {};
            obj.id = item.r.id;
            obj.img = item.r.img;
            obj.major = item.r.major;
            obj.name = item.r.n;
            obj.cook_time = item.r.cook_time;
            obj.ph = item.r.ph;
            obj.pw = item.r.pw;
            obj.tips = item.r.tips;
            obj.cookstory = item.r.cookstory;
            // console.log('obj',obj);
            arr.push(obj);
            // console.log('arr',arr);
          });
          this.$store.state.publicList = arr;
          // console.log(this.$store.state.publicList);
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.public {
  .top {
    height: 60px;
    padding: 0 15px;
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
.Food_list {
  padding: 0 15px;
  position: absolute;
  top: 40px;
  left: 0;
  width: 100%;
  ul {
    li {
      margin-top: 20px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      p {
        margin-left: 10px;
        .van-icon {
         margin-right: 10px;
        }
      }
      img {
        width: 200px;
      }

      .p_text2 {
        font-size: 14px;
        width: 150px;
        display: -webkit-box; /* 必须的，作为弹性盒子模型显示 */
        -webkit-line-clamp: 2; /* 限制显示的文本行数 */
        -webkit-box-orient: vertical; /* 设置盒子的子元素的排列方式 */
        overflow: hidden; /* 隐藏溢出的内容 */
        text-overflow: ellipsis; /* 使用省略号表示文本被截断 */
      }
    }
  }
  .message_item {
    font-size: 14px;
  }
}
</style>
